package com.yh.shiro.filter;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @Description: 自定义授权过滤器，只要满足一个角色就可以
 * @Author: 张颖辉(yh)
 * @CreateDate: 2018/5/25 17:58
 * @UpdateUser: 张颖辉(yh)
 * @UpdateDate: 2018/5/25 17:58
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
public class RolesOrFilter extends AuthorizationFilter {

    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
        Subject subject = getSubject(servletRequest, servletResponse);
        String[] roles = (String[]) o;
        if (roles==null||roles.length==0){
            return  true;
        }
        for (String role:roles){
            if (subject.hasRole(role))
                return  true;
        }
        return false;
    }
}
